import { getFormFields } from '@/utils/formUtils'
import type { FormFieldConfig } from '@/types/form'

// 表格列配置
export const tableColumns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '銷售額下限（>）',
    dataIndex: 'lowerLimit',
    customRender: ({ text }) => {
      return `$ ${Number(text).toLocaleString()}`
    },
  },
  {
    title: '銷售上限（≤）',
    dataIndex: 'upperLimit',
    customRender: ({ text }) => {
      return `$ ${Number(text).toLocaleString()}`
    },
  },
  {
    title: '總銷售分成',
    dataIndex: 'totalSalesShare',
    customRender: ({ text }) => {
      return `${text}%`
    },
  },
  {
    title: '處方分成',
    dataIndex: 'prescriptionShare',
    customRender: ({ text }) => {
      return `${text}%`
    },
  },
  {
    title: '蟲草分成',
    dataIndex: 'cordycepsShare',
    customRender: ({ text }) => {
      return `${text}%`
    },
  },
  {
    title: '幹燕分成',
    dataIndex: 'cubiloseShare',
    customRender: ({ text }) => {
      return `${text}%`
    },
  },
  {
    title: '最後修改人',
    dataIndex: 'updatedBy',
  },
  {
    title: '最後修改時間',
    dataIndex: 'updatedAt',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]

export const getTableColumns = (ruleType: number) => {
  if (ruleType == 1) {
    return tableColumns.filter((item) => item.dataIndex !== 'totalSalesShare')
  } else if (ruleType == 2) {
    return tableColumns.filter(
      (item) =>
        item.dataIndex !== 'prescriptionShare' &&
        item.dataIndex !== 'cordycepsShare' &&
        item.dataIndex !== 'cubiloseShare'
    )
  }
}

//表單
export const FormField: Record<string, FormFieldConfig> = {
  salesRange: {
    label: '銷售額範圍',
    type: 'numberRange', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入銷售額範圍',
    rules: [{ required: true, message: '請輸入銷售額範圍' }],
    tooltip: '格式說明：(下限不包含，上限包含，如10000表示≤10000)',
    addonBefore: '$',
  },
  totalSalesShare: {
    label: '總銷售額分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入總銷售分成比例',
    rules: [{ required: true, message: '請輸入總銷售分成比例', type: 'number' }],
    min: 0,
    max: 100,
    addonAfter: '%',
  },
  prescriptionShare: {
    label: '處方分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入處方分成比例',
    rules: [{ required: true, message: '請輸入處方分成比例', type: 'number' }],
    min: 0,
    max: 100,
    addonAfter: '%',
  },
  cordycepsShare: {
    label: '蟲草分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入蟲草分成',
    rules: [{ required: true, message: '請輸入蟲草分成', type: 'number' }],
    min: 0,
    max: 100,
    addonAfter: '%',
  },
  cubiloseShare: {
    label: '幹燕分成',
    type: 'number', //作用是將一個值斷言為字面量類型
    placeholder: '請輸入幹燕分成',
    rules: [{ required: true, message: '請輸入幹燕分成', type: 'number' }],
    min: 0,
    max: 100,
    addonAfter: '%',
  },
}

// 創建表單字段
export const getCreteFieldConfig = (ruleType: number) => {
  if (ruleType == 1) {
    return getFormFields(FormField, ['salesRange', 'prescriptionShare', 'cordycepsShare', 'cubiloseShare'])
  } else if (ruleType == 2) {
    return getFormFields(FormField, ['salesRange', 'totalSalesShare'])
  }
}

export const getEditFieldConfig = (ruleType: number) => {
  if (ruleType == 1) {
    return getFormFields(FormField, ['salesRange', 'prescriptionShare', 'cordycepsShare', 'cubiloseShare'])
  } else if (ruleType == 2) {
    return getFormFields(FormField, ['salesRange', 'totalSalesShare'])
  }
}
